// --- Copyright (c) 2010 Steven Hartgers ---
// Copyright (C) 2010 Steven Hartgers All rights are reserved.
// Author contact: fiinix00/at\gmail.com
// ------------------------------------------------------------------------
// This program is free software: you can redistribute it and/or modify 
// it under the terms of the GNU General Public License as published by 
// the Free Software Foundation, either version 3 of the License, or 
// (at your option) any later version. 
//  
// This program is distributed in the hope that it will be useful, 
// but WITHOUT ANY WARRANTY; without even the implied warranty of 
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the 
// GNU General Public License for more detail
//  
// You should have received a copy of the GNU General Public License 
// along with this program.  If not, see <http://www.gnu.org/licenses/>.
// ------------------------------------------------------------------------
// COPYRIGHT NOTICE: This copyright notice may NOT be removed, obscured or modified 
// without written consent from the author.
// --- End copyright notice --- 


using System;
using System.Collections;


/** A Queue for consumer-producer problem */
namespace Hack.Net.Smtp
{
    public class CPQueue
    {
        /** The data structure which helds the data */
        Queue queue = new Queue();

        /** The semaphore for filling state */
        Semaphore filling = null;

        /** The semaphore for empty state */
        Semaphore empty = null;


        /** Create a new queue of specific size */
        public CPQueue(int size)
        {
            if (size < 1)
                throw new Exception("Cannot create CSQueue with size<1.");

            filling = new Semaphore(size);
            empty = new Semaphore(0);
        }


        /** Enqueue a new object */
        public void Enqueue(object obj)
        {
            filling.Aquire();
            queue.Enqueue(obj);
            empty.Release();
        }


        /** Dequeue a saved object */
        public object Dequeue()
        {
            object obj;

            empty.Aquire();
            obj = queue.Dequeue();
            filling.Release();

            return obj;
        }
    }


}